package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.AdditionOrUpdateInspectorDTO;
import com.woniu.entity.dto.QueryInspectorDTO;
import com.woniu.entity.vo.*;
import com.woniu.entity.PageData;
import com.woniu.mapper.InspectorFeedbackMapper;
import com.woniu.mapper.InspectorMapper;
import com.woniu.mapper.OrderMapper;
import com.woniu.pojo.Inspector;
import com.woniu.pojo.InspectorFeedback;
import com.woniu.pojo.Order;
import com.woniu.service.*;
import com.woniu.utils.SMSUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Ma.Lei
* @description 针对表【t_inspector】的数据库操作Service实现
* @createDate 2022-12-16 23:27:45
*/
@Service
public class InspectorServiceImpl extends ServiceImpl<InspectorMapper, Inspector> implements InspectorService {

    @Autowired(required = false)
    private InspectorMapper inspectorMapper;
    @Autowired(required = false)
    private CarService carService;
    @Autowired(required = false)
    private RoadService roadService;
    @Autowired(required = false)
    private BerthService berthService;
    @Autowired(required = false)
    private OrderService orderService;
    @Autowired(required = false)
    private InspectorFeedbackMapper inspectorFeedbackMapper;
    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Override
    public PageData listSearch(Integer currentPage, Integer pageSize, QueryInspectorDTO queryInspectorDTO) {
//        QueryWrapper<Inspector> queryWrapper = new QueryWrapper<>();
//        if (queryInspectorDTO != null) {
//            queryWrapper.like((!StringUtils.isEmpty(queryInspectorDTO.getInspectorName())), "inspector_name", queryInspectorDTO.getInspectorName());
//            queryWrapper.like(queryInspectorDTO.getInspectorPhone() != null, "inspector_phone", queryInspectorDTO.getInspectorPhone());
//            queryWrapper.eq(queryInspectorDTO.getStatus() != null, "status", queryInspectorDTO.getStatus());
//        }
//        IPage<Inspector> page = new Page<>(currentPage,pageSize);
//        IPage<Inspector> iPage = inspectorMapper.selectPage(page, queryWrapper);
        PageHelper.startPage(currentPage,pageSize);
        List<QueryInspectorVO> inspectorVOList = inspectorMapper.listInspector(queryInspectorDTO);
        PageInfo<QueryInspectorVO> queryCarVOPageInfo = new PageInfo<>(inspectorVOList);
        return new PageData(queryCarVOPageInfo.getTotal(), queryCarVOPageInfo.getList());
    }

    @Override
    public int disabledInspector(String id, Integer status) {
        return inspectorMapper.disabledInspector(id,status);
    }

    @Override
    public QueryInspectorVO listSearchById(String id) {
        return inspectorMapper.listSearchById(id);
    }

    @Override
    public List<QueryRoadVO> selectRoadById(String id) {

        return inspectorMapper.selectRoadById(id);
    }


    @Override
    public Inspector getByPhone(String phone) {
        QueryWrapper<Inspector> wrapper = new QueryWrapper<>();
        wrapper.eq("inspector_phone",phone);
        return this.getOne(wrapper);
    }

    @Override
    public PageData findParkingRecords(String inspectorId, Date startTime, Date endTime, Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage,pageSize);
        Page<InspectorParkingRecordsVo> inspectorParkingRecordsVoPage = inspectorMapper.findParkingRecords(page, inspectorId, startTime,endTime);
        PageData pageData = new PageData(inspectorParkingRecordsVoPage.getTotal(),inspectorParkingRecordsVoPage.getRecords());
        return pageData;
    }

    @Override
    public PageData findBerthRecords(String inspectorId, String roadName, Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage, pageSize);
        Page<InspectorBerthRecordsVo> inspectorBerthRecordsVos = inspectorMapper.findBerthRecords(page, inspectorId, roadName);
        PageData pageData = new PageData(inspectorBerthRecordsVos.getTotal(),inspectorBerthRecordsVos.getRecords());
        return pageData;
    }

    @Override
    public void generateOrder(InspectorOrderVo inspectorOrderVo) {
        String code = SMSUtils.getRandomNum(19);
        Order order = new Order();
        order.setOrderCode(code);
        if(inspectorOrderVo != null){
            BeanUtils.copyProperties(inspectorOrderVo,order);
        }
        //根据car_number查找car_id
        order.setCarId(carService.getIdByNumber(inspectorOrderVo.getCarNumber()));
        //根据road_name查找road_id
        order.setRoadId(roadService.getIdByName(inspectorOrderVo.getRoadName()));
        //根据berth_code查找berth_id
        order.setBerthId(berthService.getIdByCode(inspectorOrderVo.getBerthCode()));
        //插入订单表
        boolean flag = orderService.save(order);
    }

    @Override
    public int addInspectorFeedback(InspectorFeedback inspectorFeedback) {
        return inspectorFeedbackMapper.insert(inspectorFeedback);
    }

    @Override
    public String getOrderStatusByCarNum(String carNum) {
        return inspectorMapper.getOrderStatusByCarNum(carNum);
    }

    @Override
    public Order getOrder(String carId) {
        return inspectorMapper.getOrder(carId);
    }


    @Override
    public int saveOrUpdated(AdditionOrUpdateInspectorDTO additionOrUpdateInspectorDTO) {
        Inspector inspector1 = inspectorMapper.selectById(additionOrUpdateInspectorDTO.getId());
        if (inspector1 != null){
            /** 修改 */
            Inspector inspector = new Inspector();
            inspector.setId(additionOrUpdateInspectorDTO.getId());
            inspector.setInspectorName(additionOrUpdateInspectorDTO.getInspectorName());
            inspector.setInspectorPhone(additionOrUpdateInspectorDTO.getInspectorPhone());
            inspector.setPassword(additionOrUpdateInspectorDTO.getPassword());
            inspector.setStatus(additionOrUpdateInspectorDTO.getStatus());
            inspectorMapper.deleteRoadInspector(inspector.getId());
            int i1 = inspectorMapper.updateById(inspector);
            int sum = 0;
            for (String s : additionOrUpdateInspectorDTO.getRoadId()) {
                int i = inspectorMapper.roadInsertInspectorId(s, additionOrUpdateInspectorDTO.getId());
                sum = sum+i;
            }
            if (i1 != 0){
                if (sum != 0){
                    return 1;
                }
            }
            return 0;
        }else {
            /** 添加 */
            Inspector inspector = new Inspector();
            inspector.setInspectorName(additionOrUpdateInspectorDTO.getInspectorName());
            inspector.setInspectorPhone(additionOrUpdateInspectorDTO.getInspectorPhone());
            inspector.setPassword(additionOrUpdateInspectorDTO.getPassword());
            inspector.setStatus(additionOrUpdateInspectorDTO.getStatus());
            int insert = inspectorMapper.insert(inspector);
            inspectorMapper.deleteRoadInspector(inspector.getId());
            int sum = 0;
            for (String s : additionOrUpdateInspectorDTO.getRoadId()) {
                int i = inspectorMapper.roadInsertInspectorId(s, inspector.getId());
                sum = sum+i;
            }
            if (insert != 0){
                if (sum != 0){
                    return 2;
                }
            }
            return 0;
        }
    }

    @Override
    public List<QueryOrderByInspectorIdVO> QueryOrderByInspectorId(String id) {
        return inspectorMapper.QueryOrderByInspectorIdVO(id);
    }

    @Override
    public QueryAddAndUpdeteInspectorVO listInspectorById(String id) {
        return inspectorMapper.listInspectorById(id);
    }

    @Override
    public InspectorDetailedStatisticsVO QueryInspectorDetailedStatisticsById(String id) {
        return inspectorMapper.inspectorDetailedStatisticsById(id);
    }
}




